
(function(window){
    var bind = function(callback, context){
        var args = Array.prototype.slice.call(arguments, 2);
        return function(){
            args = args.concat(arguments);
            return callback.apply(context, args);
        }
    }
    var Timer = function(){
        this.tm = null;
    }
    Timer.prototype = {
        constructor: Timer,
        init: function(callback, tick, context){
            this.stop = false;
            this.start = false;
            this.callback = callback;
            this.tick = tick || 50;
            this.context = context || window;
            
        },
        runTimer: function(callback){
            var that = this;
            tm = setTimeout(bind(function(){
                if (that.stop) {
                    tm && clearTimeout(tm);
                    that.stop = false;
                    return;
                }
                that.callback.call(this, callback);
                that.runTimer.call(that, callback, that.tick);
            }, that.context), that.tick);
        },
        stopTimer: function(){
            this.stop = true;
        }
    }
    
    window.Timer = Timer;
})(window)
